﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace FicusFactor.Watermarker
{
    public partial class WatermarkStatusWindow : Form
    {
        public WatermarkStatusWindow()
        {
            InitializeComponent();
        }

        public void Clear()
        {
            statusTextBox.Clear();
        }

        public void WriteLogEntryLine(string text, params object[] args)
        {
            text += "\r\n";
            WriteLogEntry(text, args);
        }

        public void WriteLogEntry(string text, params object[] args)
        {
            string t;
            if (args == null || args.Length == 0)
            {
                t = text;
            }
            else
            {
                t = string.Format(text, args);
            }

            ThreadPool.QueueUserWorkItem(new WaitCallback(UpdateText), t);
        }

        private void UpdateText(object o)
        {
            string t = (string)o;

            try
            {
                if (true == InvokeRequired)
                {
                    Invoke(new Action(() => statusTextBox.AppendText(t)));
                }
                else
                {
                    statusTextBox.AppendText(t);
                }
            }
            catch (InvalidOperationException)
            {
                // This can happen if the log window auto-closes before the thread to write the text finishes
            }
        }
    }
}
